/*
 * @Description:
 * @Author: fhw
 * @Date: 2023-03-27 10:20:01
 */
import {
  Controller,
  Get,
  Post,
  Body,
  Headers,
  HttpCode,
  Query,
  UseGuards,
} from '@nestjs/common';
import {
  ApiResponse,
  ApiOperation,
  ApiQuery,
  ApiBody,
  ApiBearerAuth,
  ApiTags,
} from '@nestjs/swagger';
import { IdDto } from 'src/common/dto/dto';
import { CategoryService } from './category.service';
import {
  GetCategoryDto,
  CreateCategoryDto,
  UpdateCategoryDto,
} from './dto/category.dto';
import { CategoryEntity, CategoryResult } from './entities/category.entity';
import { TokenDto } from 'src/common/auth/constants';
import { throwHttp } from 'src/utlis';
import { isTrue } from 'src/utlis/tools';
import { JwtService } from '@nestjs/jwt';
import { AuthGuard } from '@nestjs/passport';
import { LogOperation } from '../operation-record/log.decorator';
import { OperateType, OperationRecordType } from '../operation-record/entities/operation-record.entity';
@ApiBearerAuth() // swagger文档设置token
@ApiTags('分类')
@Controller('admin/category')
export class CategoryController {
  constructor(private readonly categoryService: CategoryService, private readonly jwtService: JwtService) { }

  /** 分类 */
  @HttpCode(200)
  @Get('/getCategoryPage')
  @ApiResponse({
    status: 200,
    description: '分类列表',
    type: CategoryResult,
  })
  @ApiOperation({
    tags: ['获取分类'],
    summary: '获取分类',
    operationId: 'getCategoryPage',
  })
  @UseGuards(AuthGuard('jwt')) // 自定义守卫
  getCategoryPage(@Query() params: GetCategoryDto) {
    return this.categoryService.getCategory(params);
  }
  @HttpCode(200)
  @Post('/createCategory')
  @ApiBody({
    description: '创建分类',
    type: CreateCategoryDto,
  })
  @ApiOperation({
    summary: '创建分类',
    operationId: 'createCategory',
  })
  @UseGuards(AuthGuard('jwt')) // 自定义守卫
  @LogOperation(
    {
      operateType: OperateType.add,
      operation: `创建分类`,
      name: '创建分类',
      outside: OperationRecordType.category,
      entity: CategoryEntity,
    },
  )
  async createCategory(@Body() body: CreateCategoryDto, @Headers('authorization') authorization) {
    if (!isTrue(authorization)) {
      throwHttp('获取token失败');
    }
    const token = authorization.split(' ');
    const user: TokenDto = this.jwtService.decode(token[1]) as TokenDto;
    return this.categoryService.createCategory(body, user);
  }
  @HttpCode(200)
  @Post('/updateCategory')
  @ApiBody({
    description: '更新分类信息',
    type: UpdateCategoryDto,
  })
  @ApiOperation({
    summary: '更新分类信息',
    operationId: 'updateCategory',
  })
  @UseGuards(AuthGuard('jwt')) // 自定义守卫
  @LogOperation(
    {
      operateType: OperateType.edit,
      operation: `更新分类信息`,
      name: '更新分类信息',
      outside: OperationRecordType.category,
      entity: CategoryEntity,
    },
  )
  async updateCategory(@Body() body: UpdateCategoryDto, @Headers('authorization') authorization) {
    if (!isTrue(authorization)) {
      throwHttp('获取token失败');
    }
    const token = authorization.split(' ');
    const user: TokenDto = this.jwtService.decode(token[1]) as TokenDto;
    return this.categoryService.updateCategory(body, user);
  }
  @HttpCode(200)
  @Post('/deleteCategory')
  @ApiBody({
    description: '删除分类',
    type: IdDto,
  })
  @ApiOperation({
    summary: '删除分类',
    operationId: 'deleteCategory',
  })
  @UseGuards(AuthGuard('jwt')) // 自定义守卫
  @LogOperation(
    {
      operateType: OperateType.delete,
      operation: `删除分类`,
      name: '删除分类',
      outside: OperationRecordType.category,
      entity: CategoryEntity,
    },
  )
  async deleteCategory(@Body() body: IdDto) {
    return this.categoryService.deleteCategory(body);
  }
}
